<?php 
/**
 * 登录和注册管理控制器
 */
class PublicController extends Controller{
	public function index(){
		header('Content-type:text/html;charset=utf-8');
		if (IS_POST) {
			if ($_POST['username']==''||$_POST['pwd']=='') {
				$this->success('用户名和密码不能为空',__APP__);
			}else{
				if ($_POST['pwd']==$_POST['pwded']) {
					if(strtoupper($_POST['verify']) != $_SESSION['code']){
						$this->error('验证码错误');
					}else{
						$model = M('hd_user');
						$model->exec("INSERT INTO hd_user SET username='".$_POST["username"]."',passwd='".md5($_POST["pwd"])."'");
							$this->success('注册成功',__APP__);	
					}
					
				}else{
					$this->error('两次密码输入不相同');
				}
				
			}
		
		}
	}
	public function code(){
		$code = new Code();
		$code->show();
	}
	public function login(){
		if (IS_POST) {
			header('Content-type:text/html;charset=utf-8');
			// 接收用户数据
			$username=$_POST['account'];
			$password=md5($_POST['pwd']);
			$model = new Model('hd_user');
			$rows = $model->query("SELECT * FROM hd_user where username='$username' and passwd='$password'");
				if ($rows) {
					$_SESSION['uname'] = $username;
					// 如果选了自动登录
					if (isset($_POST['auto'])) {
						setcookie(session_name(),session_id(),time()+3600*24*7,'/');
					}else{
						setcookie(session_name(),session_id(),0,'/');
					}
					$this->success('登陆成功',__APP__);
				}
		
			$this->error('用户名或者密码错误');
		}
	}
	public function out(){
		header('Content-type:text/html;charset=utf-8');
		session_unset();
		session_destroy();
		$this->success('退出成功',__APP__);
	}
}

 ?>